package fr.epsi.activities.alerte;

import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import com.example.MyLittleHotel.R;
import fr.epsi.BDD.BDD;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;

/**
 * Created by Florent Pupin on 02/06/14.
 */
public class ConsulterAlerteActivity extends ListActivity {

    private BDD bdd;
    private Cursor cListInfos, eCount,cListTache;
    private int nbEnregistrement;
    GregorianCalendar calendrier = (GregorianCalendar) GregorianCalendar.getInstance();
    GregorianCalendar calendrier2 = (GregorianCalendar) GregorianCalendar.getInstance();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.consulteralertelayout);

        try{
            bdd = new BDD();
            bdd.open(this);
            for(int i = 1; i < 25; i++){
                calendrier.add((GregorianCalendar.DAY_OF_WEEK), +1);
                String j = getDayFr(calendrier.get(calendrier.DAY_OF_WEEK));
                String jour = String.valueOf(calendrier.get(calendrier.DAY_OF_MONTH));
                String mois = String.valueOf(calendrier.get(calendrier.MONTH));
                String annee = String.valueOf(calendrier.get(calendrier.YEAR));
                String date = jour + "/" + mois + "/" + annee;
            Cursor a = bdd.getUneTache();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        if(cListInfos != null && cListInfos.moveToFirst()) {
            for (int i = 0; i < cListInfos.getColumnCount(); i++) {
                Log.i("MyHotel", cListInfos.getString(i));
            }
        }

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cListInfos, new String[]{"idtachehebdo"}, new int[]{android.R.id.text1, android.R.id.text2});
        setListAdapter(adapter);

        this.gregorianCalendar();
    }

    private void gregorianCalendar(){

        ArrayList<String> listeJours = new ArrayList();




        for(int i = 1; i < 7; i++){
            calendrier2.add((GregorianCalendar.DAY_OF_WEEK), +1);
            String j = getDayFr(calendrier2.get(calendrier2.DAY_OF_WEEK));
            String jour = String.valueOf(calendrier2.get(calendrier2.DAY_OF_MONTH));
            String mois = String.valueOf(calendrier2.get(calendrier2.MONTH));
            String annee = String.valueOf(calendrier2.get(calendrier2.YEAR));
            String date = jour + "/" + mois + "/" + annee;
            System.out.println(date);
            cListTache = bdd.getAlerteUnJour(j);
            while(cListTache.moveToNext()){
                eCount =  bdd.getVerifAlerteExistante(1,date);
                eCount.moveToNext();
                if(eCount.getInt(0) == 0){
                    bdd.ajouterUneTacheAlerte(cListTache.getInt(0), date);
                }
                else
                        System.out.println("Tache existante");

            }
        }


        /**
         *

        // add 2 months
        calendrier.add((GregorianCalendar.DAY_OF_MONTH), 2);
        System.out.println("" + calendrier.getTime());
        calendrier.add((GregorianCalendar.DAY_OF_MONTH), 2);
        System.out.println("" + calendrier.getTime());
         */
    }

    private String getDayFr(int iJourSemaine){
        String sJourSemaine = null;

        switch(iJourSemaine){
            case 1:
                sJourSemaine = "Dimanche";

                break;
            case 2:
                sJourSemaine = "Lundi";
                break;

            case 3:
                sJourSemaine = "Mardi";
                break;

            case 4:
                sJourSemaine = "Mercredi";

                break;

            case 5:
                sJourSemaine = "Jeudi";
                break;

            case 6:
                sJourSemaine = "Vendredi";
                break;

            case 7:
                sJourSemaine = "Samedi";

                break;


        }

        return sJourSemaine;
    }
}